<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/21
 * Time: 9:15
 */

namespace App\GraphQL\Mutations;

use Closure;
use App\User;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ResolveInfo;
use Rebing\GraphQL\Support\Mutation;

class CreateUserMutation extends Mutation
{

    protected $attributes = [
        'name' => 'createUser'
    ];

    public function type(): Type
    {
        return \GraphQL::type('users');//相当于表
    }

    public function args(): array
    {
        return [

            'email' => [
                'name' => 'email',
                'type' => Type::nonNull(Type::string())
            ],
            'password' => [
                'name' => 'password',
                'type' => Type::nonNull(Type::string())
            ]
        ];
    }



    public function resolve($root, $args, $context, ResolveInfo $resolveInfo, Closure $getSelectFields)
    {

    $s = <<<TEST
    
      //注意最外面不需要大括号, 小写的mutation
      mutation{
        createUser(id: "3", password: "123"){
          id
          name
        }
      }
    
TEST;

    $args['name'] = $args['email'];
    $args['password'] = bcrypt($args['password']);
    $user = User::create($args);
    if(!$user) {
        return null;
    }


    return $user;
    }
}
